package com.egene.zh.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.UiThread;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

import com.egene.zh.R;
import com.egene.zh.activity.login.LoginActivity_;
import com.egene.zh.dao.UserDao;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;


@EActivity(R.layout.activity_start)
public class StartActivity extends AppCompatActivity {

    private boolean isExit = false;


    @Bean
    UserDao userDao;

    @AfterInject
    void deal() {
        SharedPreferences pre = getSharedPreferences("iii", MODE_PRIVATE);
        int count = pre.getInt("count", 0);
        if (count < 1) {
            count++;
            pre.edit().putInt("count", count).commit();
            Intent intent = new Intent(this, GuideActivity_.class);
            startActivity(intent);
            finish();
        } else {
            waiting();
        }
    }


    @Background
    void waiting() {

        try {
//            new TT().doSomeThing();
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (!isExit) {
            redirectTo();
        }
    }


    @UiThread
    void redirectTo() {
        Intent intent;
//        if(GUIDE_VERSION.equals(configPref.guidePageVersion().get())){
//            intent = new Intent(this, LoginActivity_.class);
//        }else {
//            intent = new Intent(this, GuideActivity_.class);
//            configPref.guidePageVersion().put(GUIDE_VERSION);
//        }
//
//        SharedPreferences pre = getSharedPreferences("iii", MODE_PRIVATE);
//        int count = pre.getInt("count", 0);
//
//        if (count < 5) {
//            intent = new Intent(this, GuideActivity_.class);
//            count++;
//            pre.edit().putInt("count", count).commit();
//        } else {
//            intent = new Intent(this, LoginActivity_.class);
//        }
//

        intent = new Intent(this, LoginActivity_.class);
        startActivity(intent);
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            isExit = true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
